<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>我的订单</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        :root {
            --primary: #4CAF50;
            --primary-light: #E8F5E9;
            --primary-dark: #388E3C;
            --accent: #8BC34A;
            --bg-color: #F8FBF8;
            --card-color: #FFFFFF;
            --text-dark: #2E7D32;
            --text-light: #757575;
            --border: #C8E6C9;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Helvetica Neue', Arial, 'Microsoft YaHei', sans-serif;
        }

        body {
            background-color: var(--bg-color);
            color: #333;
            padding: 30px;
            line-height: 1.6;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 1px solid var(--border);
        }

        h1 {
            font-size: 24px;
            font-weight: 500;
            color: var(--primary-dark);
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .card {
            background-color: var(--card-color);
            border-radius: 10px;
            box-shadow: 0 3px 20px rgba(0, 150, 0, 0.08);
            overflow: hidden;
            border: 1px solid var(--border);
        }

        .order-list {
            padding: 0;
        }

        .order-item {
            padding: 20px;
            border-bottom: 1px solid var(--border);
            transition: background-color 0.2s;
        }

        .order-item:hover {
            background-color: rgba(76, 175, 80, 0.05);
        }

        .order-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 12px;
            font-weight: 500;
        }

        .order-details {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 15px;
            margin-bottom: 15px;
        }

        .detail-group {
            display: flex;
            flex-direction: column;
        }

        .detail-label {
            font-size: 13px;
            color: var(--text-light);
            margin-bottom: 5px;
        }

        .detail-value {
            font-weight: 500;
        }

        .order-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px dashed var(--border);
        }

        .btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 8px 16px;
            border-radius: 6px;
            font-weight: 500;
            transition: all 0.2s ease;
            background-color: var(--primary);
            color: white;
            border: none;
            cursor: pointer;
            font-size: 14px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .btn:hover {
            background-color: var(--primary-dark);
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.15);
        }

        .btn-outline {
            background-color: transparent;
            border: 1px solid var(--primary);
            color: var(--primary);
        }

        .btn-outline:hover {
            background-color: var(--primary-light);
        }

        .status-badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 500;
        }

        .status-pending {
            background-color: rgba(255, 193, 7, 0.1);
            color: #FF8F00;
            border: 1px solid #FFC107;
        }

        .status-completed {
            background-color: rgba(76, 175, 80, 0.1);
            color: var(--primary-dark);
            border: 1px solid var(--primary);
        }

        .status-cancelled {
            background-color: rgba(244, 67, 54, 0.1);
            color: #D32F2F;
            border: 1px solid #F44336;
        }

        .no-orders {
            text-align: center;
            padding: 40px;
            color: var(--text-light);
        }

        .no-orders i {
            font-size: 48px;
            color: var(--border);
            margin-bottom: 15px;
        }

        .no-orders h3 {
            margin-bottom: 15px;
            color: var(--text-dark);
        }

        /* 新增的支付按钮样式 */
        .action-buttons {
            display: flex;
            gap: 10px;
        }

        .pay-btn {
            background-color: #2196F3;
        }

        .pay-btn:hover {
            background-color: #0b7dda;
        }

        @media (max-width: 768px) {
            body {
                padding: 15px;
            }

            .header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .order-details {
                grid-template-columns: 1fr 1fr;
            }

            .order-footer {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }

            .action-buttons {
                width: 100%;
                justify-content: flex-end;
            }
        }

        @media (max-width: 480px) {
            .order-details {
                grid-template-columns: 1fr;
            }

            .action-buttons {
                flex-direction: column;
                gap: 8px;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1><i class="fas fa-clipboard-list"></i> 我的订单</h1>
        <a href="${pageContext.request.contextPath}/foods/list" class="btn">
            <i class="fas fa-plus"></i> 继续购物
        </a>
    </div>

    <div class="card">
        <div class="order-list">
            <c:choose>
                <c:when test="${empty orders}">
                    <div class="no-orders">
                        <i class="fas fa-clipboard-list"></i>
                        <h3>您还没有任何订单</h3>
                        <a href="${pageContext.request.contextPath}/products" class="btn btn-outline">去选购商品</a>
                    </div>
                </c:when>
                <c:otherwise>
                    <c:forEach items="${orders}" var="order">
                        <form action="${pageContext.request.contextPath}/api/alipay/pay" method="post">
                            <input type="hidden" name="orderId" value="${order.orderid}">
                            <div class="order-item">
                                <div class="order-header">
                                    <span>订单号: ${order.orderid}</span>
                                    <span class="status-badge status-${order.orderstatus}">
                                        <c:choose>
                                            <c:when test="${order.orderstatus == 'pending'}">待处理</c:when>
                                            <c:when test="${order.orderstatus == 'completed'}">已完成</c:when>
                                            <c:when test="${order.orderstatus == 'cancelled'}">已取消</c:when>
                                        </c:choose>
                                    </span>
                                </div>

                                <div class="order-details">
                                    <div class="detail-group">
                                        <span class="detail-label">用户ID</span>
                                        <span class="detail-value">${order.userid}</span>
                                    </div>

                                    <div class="detail-group">
                                        <span class="detail-label">食谱ID</span>
                                        <span class="detail-value">${order.recipeid}</span>
                                    </div>

                                    <div class="detail-group">
                                        <span class="detail-label">数量</span>
                                        <span class="detail-value">${order.num}</span>
                                    </div>

                                    <div class="detail-group">
                                        <span class="detail-label">总价</span>
                                        <span class="detail-value">¥${order.price}</span>
                                    </div>

                                    <div class="detail-group">
                                        <span class="detail-label">订单状态</span>
                                        <span class="detail-value">${order.orderstatus}</span>
                                    </div>

                                    <div class="detail-group">
                                        <span class="detail-label">下单时间</span>
                                        <span class="detail-value">${order.ordertime}</span>
                                    </div>

                                    <div class="detail-group">
                                        <span class="detail-label">配送时间</span>
                                        <span class="detail-value">
                                            <c:choose>
                                                <c:when test="${empty order.deliverytime}">未配送</c:when>
                                                <c:otherwise>${order.deliverytime}</c:otherwise>
                                            </c:choose>
                                        </span>
                                    </div>
                                </div>

                                <div class="order-footer">
                                    <span class="text-muted">订单创建时间: ${order.ordertime}</span>
                                    <div class="action-buttons">
                                            <!-- 去支付按钮 -->
                                            <button type="submit" class="btn pay-btn">
                                                <i class="fas fa-credit-card"></i> 去支付
                                            </button>
                                            <!-- 取消订单按钮 -->
                                            <button type="button" class="btn btn-outline cancel-btn" data-id="${order.orderid}">
                                                <i class="fas fa-times"></i> 取消订单
                                            </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </c:forEach>
                </c:otherwise>
            </c:choose>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(function() {
        $('.cancel-btn').click(function() {
            if(confirm('确定要取消此订单吗？')) {
                const btn = $(this);
                btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 处理中');

                $.post('${pageContext.request.contextPath}/order/cancel', {
                    orderId: btn.data('id')
                }, function(res) {
                    if(res === 'success') {
                        location.reload();
                    } else if(res === 'unauthorized') {
                        alert('请先登录');
                        window.location.href = '/login';
                    } else {
                        alert('操作失败');
                        btn.prop('disabled', false).html('<i class="fas fa-times"></i> 取消订单');
                    }
                });
            }
            return false;
        });

        // 支付按钮点击事件（可选）
        $('.pay-btn').click(function() {
            $(this).html('<i class="fas fa-spinner fa-spin"></i> 跳转支付中...');
        });
    });
</script>
</body>
</html>